package sk.octopuss.archery.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sk.octopuss.archery.dao.CategoryDao;
import sk.octopuss.archery.domain.Category;
import sk.octopuss.archery.service.AbstractGenericService;
import sk.octopuss.archery.service.CategoryService;

@Service
public class CategoryServiceImpl extends AbstractGenericService implements CategoryService{

	@Autowired
	private CategoryDao categoryDao;
	
	@Override
	public Category find(final Long id) {
		return categoryDao.find(id);
	}

	@Override
	public List<Category> findAll() {
		return categoryDao.findAll();
	}

	@Override
	public Category findByName(final String name) {
		return categoryDao.findByName(name);
	}

	@Override
	public List<Category> findAllByName(final String name) {
		// TODO Auto-generated method stub
		return null;
	}

	

	@Override
	public List<Category> getParentCategoriesFromTop(final Category category) {
		final List<Category> categories = categoryDao.getParentCategories(category);
		Collections.reverse(categories);
		return categories;
	}

	@Override
	public List<Category> getParentCategories(final Category category) {
		return categoryDao.getParentCategories(category);
	}

	@Override
	public Map<Category,List<String>> getAllTreePaths() {
		final List<Category> categories = categoryDao.findAll();
		
		final Map<Category, List<String>> out = new LinkedHashMap<Category, List<String>>();
		List<String> catsInTree;
		for (final Category category : categories) {
			catsInTree = new ArrayList<String>();
			Category cat = category;
			while (cat != null) {
				catsInTree.add(cat.getName());
				cat = cat.getParent();
			}
			out.put(category,catsInTree);
		}
		return out;
	}

	@Override
	public List<String> getTreePath(final Category category) {
		final List<String> catsInTree = new ArrayList<String>();
		Category cat = category;
		while (cat!= null) {
			catsInTree.add(cat.getName());
			cat = cat.getParent();
		}
		return catsInTree;
	}

	@Override
	public long save(final Category entity) {
		return categoryDao.save(entity);
	}

	@Override
	public void remove(final Category entity) {
		// TODO Auto-generated method stub
		
	}

	

}
